<?php

class Tagging_model extends Model {


    function Tagging_model() {
        // Call the Model constructor
        parent::Model();
        
        // load DB
        $this->load->database();
    }
    
    
    /*
        GET TAGS
    */
    function get_root_tags() {
        // returns all root_tags
       $query = $this->db->get('root_tags');
       return $query->result();
    }
    
    function get_root_tag($id) {
        // returns specific root tag
        $this->db->select('*');
        $this->db->from('root_tags');
        $this->db->where('id', $id);

        $query = $this->db->get();
        return $query->result();
    }
    
    function get_tags() {
        // returns all tags
        $query = $this->db->get('tags');
        return $query->result();
    }
    
    function get_tag($id) {
        // returns specific tag
        $this->db->select('*');
        $this->db->from('tags');
        $this->db->where('id', $id);

        $query = $this->db->get();
        return $query->result();
    }
    
    function get_related_tags() {
        // returns all tag relations
        $query = $this->db->get('related_tags');
        return $query->result();
    }
    
    function get_related_tag($tag_id) {
        //returns all relations for a tag where tag = tag_a or tag_b
        $this->db->select('*');
        $this->db->from('related_tags');
        $this->db->where('tag_id_a', $tag_id);
        $this->db->orwhere('tag_id_b', $tag_id);
        
        $query = $this->db->get();
        return $query->result();
        
    }
    
    function get_related_tag_by_a($tag_id) {
        // returns only relations where tag = tag_a
        $this->db->select('*');
        $this->db->from('related_tags');
        $this->db->where('tag_id_a', $tag_id);

        $query = $this->db->get();
        return $query->result();
    }
    
    function get_related_tag_by_b($tag_id) {
        // returns only relations where tag = tag_b
        $this->db->select('*');
        $this->db->from('related_tags');
        $this->db->where('tag_id_b', $tag_id);

        $query = $this->db->get();
        return $query->result();
    }
    
    
    /*
        ADD AND EDIT TAGS
    */
    function add_root_tag($root_tag_parent_id, $tag) {
        if ($tag) {
            $data = array(
               'root_tag_parent_id' => $root_tag_parent_id,
               'tag' => $tag
            );
            
            $this->db->insert('root_tags', $data);   
        }        
    }

    function edit_root_tag($id, $tag, $root_tag_parent_id) {
        if ($id && $tag) {
            $data = array(
                'tag' => $tag,
                'root_tag_parent_id' => $root_tag_parent_id
            );

            $this->db->where('id', $id);
            $this->db->update('root_tags', $data);
        }       
    }
    
    function add_tag($root_tag_id, $tag) {
        if ($tag) {
            $data = array(
               'root_tag_id' => $root_tag_id,
               'tag' => $tag
            );
            
            $this->db->insert('tags', $data);   
        }        
    }
    
    function edit_tag($id, $tag, $root_tag_id) {
        if ($id && $tag) {
            $data = array(
                'tag' => $tag,
                'root_tag_id' => $root_tag_id
            );

            $this->db->where('id', $id);
            $this->db->update('tags', $data);
        }
    }
    
    function add_tag_relation($tag_id_a, $tag_id_b) {
        if ($tag_id_a && $tag_id_b) {
            $data = array(
               'tag_id_a' => $tag_id_a,
               'tag_id_b' => $tag_id_b
            );
            
            $this->db->insert('related_tags', $data);   
        }        
    }

    function edit_tag_relation($id, $tag_id_a, $tag_id_b) {
        if ($id && $tag_id_a && $tag_id_b) {
            $data = array(
                'tag_id_a' => $tag_id_a,
                'tag_id_b' => $tag_id_b
            );

            $this->db->where('id', $id);
            $this->db->update('related_tags', $data);
        }       
    }
      
      
      
      
      
      
}


?>